package com.ymatou.iapi.marketingmanage.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.marketingmanage.parameter.GetProductPromotionPolicyBean;
import com.ymatou.iapi.marketingmanage.parameter.PromotionPolicy;
import com.ymatou.iapi.marketingmanage.parameter.PromotionValue;
import com.ymatou.iapi.marketingmanage.service.GetProductPromotionPolicyCall;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.database.mongo.YmtBuyerProductWapper;
/**
*获取商品的促销活动信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductPromotionPolicy{
	private static GetProductPromotionPolicyBean getproductpromotionpolicyBean;
	private static GetProductPromotionPolicyCall getproductpromotionpolicyCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品的促销活动信息");
	}

	@Before
	public void caseUp() {
		getproductpromotionpolicyBean=new GetProductPromotionPolicyBean();
		getproductpromotionpolicyCall=new GetProductPromotionPolicyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductpromotionpolicyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyData(GetProductPromotionPolicyCall getproductpromotionpolicyCall2, CreateSellerPromotionBean cspb) {
		JsonObject json = getproductpromotionpolicyCall2.getJsonObject().getAsJsonObject("Data");
		JsonObject recjson=null;
		if( !json.get("ProductPromotionPolicy").isJsonNull()){
			recjson = json.get("ProductPromotionPolicy").getAsJsonObject();
		}
		PromotionPolicy recp=(PromotionPolicy) FormatUtil.GSONToObject(json.get("ProductPromotionPolicy").toString(), PromotionPolicy.class);
		PromotionPolicy sendp = cspb.getPromotionInfo();
		Logger.debug(FormatUtil.beanToGSONString(sendp));
		Logger.debug(FormatUtil.beanToGSONString(recp));
		if(cspb.getPromotionInfo().getProductIds()!=null&&cspb.getPromotionInfo().getProductIds().size()==1){
			Logger.verifyEquals(recjson.get("ProductId").getAsString(),cspb.getPromotionInfo().getProductIds().get(0),"验证productid");
		}
		Logger.verifyEquals(recp.getPromotionName(),sendp.getPromotionName(),"验证PromotionName");
		Logger.verifyEquals(recp.getPromotionDesc(),sendp.getPromotionDesc(),"验证PromotionDesc");
		Logger.verifyEquals(recp.getPromotionBeginTime().substring(0,12),sendp.getPromotionBeginTime().substring(0,12),"验证PromotionBeginTime");
		Logger.verifyEquals(recp.getPromotionEndTime().substring(0,12),sendp.getPromotionEndTime().substring(0,12),"验证PromotionEndTime");
		Logger.verifyEquals(recp.getPromotionType(),sendp.getPromotionType(),"验证PromotionType");
		Logger.verifyEquals(recp.getIsSameProductUse(),sendp.getIsSameProductUse(),"验证IsSameProductUse");
		PromotionValue spv = sendp.getPromotionValues().get(0);
		PromotionValue rpv = recp.getPromotionValues().get(0);
		Integer type = sendp.getPromotionType();
		if (type == 1) {
			Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(),spv.getValue(), "验证dv");

		}
		// 折扣的时候/10
		else if (type == 2) {
			Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)) ,"验证dv");

		} else if (type == 3) {
			Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue(), "验证dv");
		} else if (type == 4) {
			Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition() ,"验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)), "验证dv");

		} else if (type == 5) {
			Logger.verifyEquals(2, spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(new BigDecimal(0.5), spv.getValue(), "验证dv");

		}
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_001() {
		Logger.start(true,"指定商品获取商品的促销活动信息成功活动type1");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_002() {
		Logger.start(true,"不输入sellerid，失败");
		try {
//			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
//			cspb.getPromotionInfo().setIsSpecifyProduct(true);
//			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
//			cspb.getPromotionInfo().getProductIds().add(ProductId);
//			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(null);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyIsContains("无效的买手编号", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_003() {
		Logger.start(true,"不输入ProductId，失败");
		try {
//			getproductpromotionpolicyBean.setProductId(ProductId);
			int sellerid=106;
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyIsContains("无效的产品编号", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_004() {
		Logger.start(true,"输入sellerid和ProductId不匹配,失败");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid+1);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");	
			Logger.verifyEquals(null, getproductpromotionpolicyCall.getString("ProductPromotionPolicy"), "验证ProductPromotionPolicy");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_005() {
		Logger.start(true,"排除商品不会获取到促销活动信息");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(false);
			cspb.getPromotionInfo().setIsLimitProduct(true);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(null, getproductpromotionpolicyCall.getString("ProductPromotionPolicy"), "验证ProductPromotionPolicy");
//			verifyData(getproductpromotionpolicyCall, cspb);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_006() {
		Logger.start(true,"全场商品获取到促销活动信息");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(false);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_007() {
		Logger.start(true,"指定商品获取商品的促销活动信息成功活动type2");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfCountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_008() {
		Logger.start(true,"指定商品获取商品的促销活动信息成功活动type3");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfAmountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_009() {
		Logger.start(true,"指定商品获取商品的促销活动信息成功活动type4");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_010() {
		Logger.start(true,"指定商品获取商品的促销活动信息成功活动type5");
		try {
			int sellerid=106;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.HalfIfCountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			cspb.getPromotionInfo().setIsLimitProduct(false);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String ProductId=bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(ProductId);
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			
			getproductpromotionpolicyBean.setProductId(ProductId);
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();			
			Logger.verifyEquals("操作成功", getproductpromotionpolicyCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getproductpromotionpolicyCall.getString("BCode"), "验证BCode");
			
			verifyData(getproductpromotionpolicyCall, cspb);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}